<?php
//+--------------------------------------------
//|描述  HTTP server
//|--------------------------------------------
//|Created by PhpStorm.
//| Author: DRG
//+-------------------------------------------
//|Date: 2018/6/5
//| Time: 14:16
//+--------------------------------------------

class Http{
    CONST HOST = "0.0.0.0";
    CONST PORT = 80;
    
    public $http = null;
    public function __construct()
    {
        $this->http = new swoole_http_server(self::HOST, self::PORT);
    
        $this->http->set(
            [
                "enable_static_handler" => true,
                "document_root"         => '/home/wwwroot/thinkphp/public/static',
                'worker_num' => 4,
                'task_worker_num' => 4,
            ]
        );
        $this->http->on("WorkerStart", [$this, 'onWorkerStart']);
        $this->http->on("request", [$this, 'onRequest']);
        //task http投递事件
        $this->http->on("task", [$this, 'onTask']);
        $this->http->on("finish", [$this, 'onFinish']);
        $this->http->on("close", [$this, 'onClose']);
        $this->http->start();
    }
    
    /**
     * work启动时候就加载框架内容
     * @param $server
     * @param $work_id
     * @author DRG
     */
    public function OnWorkerStart( $server, $work_id)
    {
        //定义应用目录
        define('APP_PATH', __DIR__ . '/../application/');
        //加载核心文件
//        require __DIR__ . '/../thinkphp/base.php';
        require __DIR__ . '/../thinkphp/start.php';// 这样所有地方都能使用框架的核心文件和类库
    
    }
    
    public function onRequest($request,$response)
    {
        $_GET = [];
        $_POST = [];
        $_SERVER = [];
        $_COOKIE = [];
    
        if(isset($request->server)) {
            foreach ($request->header as $k => $v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        if(isset($request->get)) {
            foreach ($request->get as $k => $v) {
                $_GET[$k] = $v;
            }
        }
        if(isset($request->post)) {
            foreach ($request->post as $k => $v) {
                $_POST[$k] = $v;
            }
        }
        $_POST['SWOOLE_HTTP_SERVER'] = $this->http;//方便后面框架里使用task任务
        ob_start();
        // 执行应用并响应
        try{
            think\Container::get('app',[APP_PATH])->run()->send();
        }catch (\Exception $e){
            //todo
        }
        $res = ob_get_contents();
        ob_end_clean();
        //$response->cookie('duruigang','xxxxxx',time()+1800);//设置cookie
        $response->end($res);// 将tp 应用的内容发送到浏览器
    }
    
    /**
     * @param $serv
     * @param $task_id
     * @param $src_worker_id
     * @param $data
     * @author DRG
     */
    public function onTask( $serv,  $task_id,  $src_worker_id,  $data)
    {
        //分发  task 任务机制  让不同的任务走不同的逻辑
        
        $obj = new \app\common\lib\task\Task();
        $method = $data['method'];
        $flag = $obj->$method($data['data']);
        /*try{
            $obj = new app\common\lib\ali\Sms();
            $resposer = $obj::sendSms($data['phone'], $data['code']);
        }catch (\Exception $e){
            //todo
            echo $e->getMessage();
            //return app\common\lib\Util::show(config('code.empty'),'阿里大鱼内部异常');
        }*/
       // print_r($resposer);//短信平台返回的数据
        /**
         * stdClass Object
         * (
         *   [Message] => OK
         *   [RequestId] => FB72D4D9-CDFB-4328-BA9E-F18623608A61
         *   [BizId] => 340017628244846112^0
         *   [Code] => OK
         * )
         */
        return $flag;//告诉work
        //return 'on task finish ';
    }
    
    /**
     * @param $serv
     * @param $taskId
     * @param $data    是onTask  中return的内容
     * @author DRG
     */
    public function onFinish($serv, $taskId, $data)
    {
        echo "taskId: {$taskId}\n";
        echo "finish-data-success: {$data}\n";
    }
    /**
     * 关闭连接
     * @param $ws
     * @param $fd
     * @author DRG
     */
    public function onClose($ws, $fd)
    {
        echo "clientid:{$fd}";
    }
}

new Http();